package org.leetcode.easy.leetcode28;

import javax.print.DocFlavor;
import java.util.HashMap;

public class Solution
{
    public  int strStr2(String haystack, String needle)
    {

        if (needle.length()>haystack.length())
        {
            return -1;
        }

        int left = 0;

        int right = left+needle.length()-1;


        while (right<=haystack.length()-1)
        {
            String substring = haystack.substring(left, right+1);

            if (substring.equals(needle))
            {
                return left;
            }else {
                left++;
                right++;
            }

        }

        return  -1;
    }


    public static int strStr(String haystack, String needle)
    {

        int lengthOne = haystack.length();
        int lengthTwo = needle.length();
        int index = 0;



        if (lengthOne<lengthTwo)
        {
            return -1;
        }
        String substring = haystack.substring(index, lengthTwo);

        while (index+lengthTwo<=lengthOne)
        {
            if (substring.equals(needle))
            {
                return index;
            }else {
                index=index+1;
                if (index+lengthTwo>lengthOne)
                    return -1;
                substring=haystack.substring(index,lengthTwo+index);
            }
        }


       return -1;

    }

    public static void main(String[] args) {


        String haystack = "aaleetcode";
        String needle = "leet";
//        int i = strStr(haystack, needle);
//        System.out.println(i);

        Solution solution = new Solution();
        int i1 = solution.strStr2(haystack, needle);
        System.out.println(i1);


    }
}
